﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MovieRental.Entities;
using MovieRental.SqlRepositoryLibrary.Helpers;
using System.Data;
using System.Data.SqlClient;

namespace MovieRental.SqlRepositoryLibrary.Converters
{
    public class MovieConverter : BaseConverter<MovieInfo, Guid>
    {
        protected override SqlEntityMapping SetUpMapping()
        {
            var mapping = new SqlEntityMapping("Movies", "Id");
            mapping.AddProperty("Id", "Id", SqlDbType.UniqueIdentifier);
            mapping.AddProperty("Title", "Title", SqlDbType.NVarChar);
            mapping.AddProperty("ImdbRating", "ImdbRating", SqlDbType.Float);
            mapping.AddProperty("WebPage", "WebPage", SqlDbType.NVarChar);
            return mapping;
        }

        protected override void SetUpPropertyValues(MovieInfo entity)
        {
            SetProperty("Id", entity.Id);
            SetProperty("Title", entity.Title);
            SetProperty("ImdbRating", entity.ImdbRating);
            SetProperty("WebPage", entity.WebPage);
        }

        public override MovieInfo ConvertFromDataReader(SqlDataReader reader)
        {
            Guid id = (Guid) GetValueForProperty(reader, "Id");
            MovieInfo movie = new MovieInfo(id);
            movie.Title = (string)GetValueForProperty(reader, "Title");
            movie.ImdbRating = (double)GetValueForProperty(reader, "ImdbRating");
            movie.WebPage = (string)GetValueForProperty(reader, "WebPage");
            return movie;
        }
    }
}
